package com.lin.project.cloudnative.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.project.cloudnative.common.ErrorCode;
import com.lin.project.cloudnative.constant.CommonConstant;
import com.lin.project.cloudnative.exception.BusinessException;
import com.lin.project.cloudnative.mapper.TestTaskMapper;
import com.lin.project.cloudnative.model.dto.testTask.TestTaskQueryRequest;
import com.lin.project.cloudnative.model.entity.TestTask;
import com.lin.project.cloudnative.service.TestTaskService;
import com.lin.project.cloudnative.util.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author 林怀旭
* @description 针对表【test_task(测试任务)】的数据库操作Service实现
* @createDate 2023-11-12 21:37:45
*/
@Service
public class TestTaskServiceImpl extends ServiceImpl<TestTaskMapper, TestTask>
    implements TestTaskService{

    @Override
    public void validTestTask(TestTask testTask, boolean add) {
        if(testTask==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = testTask.getId();
        Integer count = testTask.getCount();
        String testcaselist = testTask.getTestCaseList();
        String ruleset = testTask.getRuleSet();
//        Integer status = testTask.getStatus();
        Integer staticStatus = testTask.getStaticStatus();
        Integer dynamicStatus = testTask.getDynamicStatus();
//        Integer time = testTask.getTime();
        String remark = testTask.getRemark();
        Long userid = testTask.getUserId();

        //创建时，所有参数必须非空
        if(add){
            if(StringUtils.isAnyBlank(testcaselist,ruleset)){
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
//        if(StringUtils.isNotBlank(code) && code.length() > 8192){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"代码过长");
//        }
//        if(language!=null && !PostReviewStatusEnum.getValues().contains(reviewStatus)){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        if(age!=null && (age<18 || age>100)){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"年龄不符合要求");
//        }
//        if(gender!=null && !PostGenderEnum.getValues().contains(gender)){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR,"年龄不符合要求");
//        }
    }

    @Override
    public QueryWrapper<TestTask> getQueryWrapper(TestTaskQueryRequest request) {
        QueryWrapper<TestTask> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }
        String taskName = request.getTaskName();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();
        queryWrapper.like(StringUtils.isNotBlank(taskName), "taskName", "%" + taskName + "%");
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




